<template>
	<view class="type">
		<NavBar title="分类"></NavBar>
		<view class="content">
			<Search v-model="form.search" @change="getList"></Search>
			<div class="tabs">
				<Tabs :tabs="tabs" @change="handleTabsChange" :active="active"></Tabs>
			</div>
			<view class="filter" v-if="tabs[active] && tabs[active].children">
				<view class="li" v-for="(item, index) in tabs[active].children" :key="index">
					<text @click="changeClass(index)">{{ item.lable }}</text>
					<image src="@/static/icons/arrow.png"></image>
				</view>
			</view>
			<up-empty mode="list" v-if="!list.length"></up-empty>
			<view class="list">
				<view class="item" v-for="(item, index) in list" :key="index">
					<image :src="item.cover" mode="aspectFill"></image>
					<view class="cont">
						<view class="title">{{ item.title }}</view>
						<view class="text">{{ item.content }}</view>
						<view class="foot">
							<view class="price">¥ {{ item.price }}</view>
							<view class="buttons">
								<view class="btn primary" @click="$gotoUrl('/pages/index/goodDetail?id=' + item.id)">
									立即购买</view>
								<view class="btn default" @click="$gotoUrl('/pages/index/goodDetail?id=' + item.id)">
									查看详情</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-picker :show="show" :columns="[categoryDataList]" keyName="lable" @confirm="confirm" @cancel="close"></u-picker>
	</view>
</template>
<script setup>
import Search from '@/components/Search.vue';
import Tabs from '@/components/Tabs.vue';
import uPicker from "@/uni_modules/uview-plus/components/u-picker/u-picker.vue";
import {
	getClassifyList,
	categoryList
} from '@/api/classify.js'
import {
	reactive,
	toRefs
} from 'vue'
const state = reactive({
	form: {
		id: "",
		page: 1,
		limit: 10,
		search: "",
	},
	tabs: [],
	list: [],
	active: 0,
	categoryDataList: [],
	show: false,
	clsssIndex: 0
})
function changeClass (index) {
	state.clsssIndex = index
	state.categoryDataList = state.tabs[state.active].children[index].children
	console.log(state.categoryDataList);
	state.show = true

}
function confirm (e) {
	if (state.form.id == e.value[0].id) {
		state.form.id = ''
	} else {
		state.form.id = e.value[0].id
	}
	show.value = false;
	getList()
}
function close () {
	state.show = false
}
function handleTabsChange (item) {
	state.active = item.value
}

function getList () {
	uni.showLoading({
		title: '加载中'
	});
	getClassifyList(state.form).then(res => {
		state.list = res.data.data
		uni.hideLoading();
	})
}
async function getCategoryList () {
	const res = await categoryList()
	state.tabs = res.data.map((item, index) => Object.assign({ ...item, value: index, label: item.lable }))
}

getCategoryList()
getList()
const {
	tabs,
	active,
	loading,
	list,
	categoryDataList,
	show,
	clsssIndex,
	form
} = toRefs(state)
</script>
<style lang="scss" scoped>
.content {
	border-radius: 22rpx 22rpx 0rpx 0rpx;
	background: #FFFFFF;
	padding: 36rpx;
	position: relative;
	z-index: 1;

	.tabs {
		margin: 28rpx 0;
	}

	.filter {
		display: flex;

		.li {
			margin: 10rpx 0rpx;
			font-weight: 500;
			font-size: 24rpx;
			color: #333333;
			line-height: 28rpx;
			position: relative;

			image {
				width: 28rpx;
				height: 15rpx;
			}

			.spin1,
			.spin2 {
				transform: rotate(180deg);
			}

			.dropdown-box {
				position: absolute;
				background-color: #fff;
				top: 50rpx;
				left: -10rpx;
				background-color: #f9f9f9;
				box-shadow: 0px 8rpx 16rpx 0px rgba(0, 0, 0, 0.2);
				width: 140rpx;
				z-index: 9999;

				.item {
					color: #333333;
					margin: 20rpx;
				}

				.activeClass {
					color: #58DBCF;
				}
			}
		}

		.li+.li {
			margin-left: 26rpx;
		}
	}

	.list {
		margin-top: 37rpx;

		.item {
			display: flex;
			margin-bottom: 36rpx;

			image {
				width: 155rpx;
				height: 155rpx;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-right: 24rpx;
			}

			.cont {
				flex: 1;

				.title {
					font-weight: bold;
					font-size: 28rpx;
					color: #333333;
					line-height: 33rpx;
					margin-bottom: 10rpx;
				}

				.text {
					font-weight: bold;
					font-size: 20rpx;
					color: #666666;
					line-height: 23rpx;
					margin-bottom: 28rpx;
				}
			}

			.foot {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.price {
					font-weight: 800;
					font-size: 30rpx;
					color: #FF0000;
					line-height: 35rpx;
				}

				.buttons {
					.btn {
						display: inline-block;
						padding: 10rpx 20rpx;
						border-radius: 55rpx 55rpx 55rpx 55rpx;
						font-weight: 500;
						font-size: 20rpx;
						line-height: 23rpx;
					}

					.btn.primary {
						background-color: #58DBCF;
						color: #fff;
					}

					.btn.default {
						color: #58DBCF;
						border: 1rpx solid #58DBCF;
					}

					.btn+.btn {
						margin-left: 18rpx;
					}
				}
			}
		}
	}
}
</style>